package com.xqboss.apps.service.nft;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.nft.NftTransferConfig;
import com.xqboss.apps.dto.nft.NftTransferDto;
import com.xqboss.apps.mapper.nft.NftTransferConfigMapper;
import com.xqboss.apps.service.consumer.ConsumerTeamGiveRecordService;
import com.xqboss.common.exception.ServiceException;
import com.xqboss.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * nft藏品-转赠配置 服务实现类
 * </p>
 *
 * @author 木鱼
 * @since 2024-03-07
 */
@Service
@Slf4j
public class NftTransferConfigService extends ServiceImpl<NftTransferConfigMapper, NftTransferConfig> {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ConsumerTeamGiveRecordService consumerTeamGiveRecordService;

    /**
     * 根据藏品 装备 宝盒id获取转赠配置
     */
    public NftTransferConfig getTransferConfig(Long id) {
        return getOne(Wrappers.lambdaQuery(NftTransferConfig.class)
                .eq(NftTransferConfig::getSourceId, id));
    }

    /**
     * 根据转赠配置id获取转赠配置
     * 校验转赠配置是否存在
     */
    public NftTransferDto verifyTransfer(Long userId, NftTransferDto params) {
        if (userId.equals(params.getToUserId())) {
            throw new ServiceException("不能转赠给自己");
        }
        // 当前用户id转为string类型
        String currUserId = String.valueOf(userId);
        NftTransferConfig config = getOne(Wrappers.lambdaQuery(NftTransferConfig.class)
                .eq(NftTransferConfig::getId, params.getTransferConfigId())
                .eq(NftTransferConfig::getSourceId, params.getNftId())
                .eq(NftTransferConfig::getType, params.getType().getValue()));
        if (Objects.isNull(config)) {
            throw new ServiceException("转赠不存在");
        }
        if (!config.getIsTransfer()) {
            throw new ServiceException("转赠未开启");
        }
        // 校验藏品编号是否重复
        List<String> nftNumberStr = params.getNftNumberStr();
        if (nftNumberStr.size() != nftNumberStr.stream().distinct().count()) {
            throw new ServiceException("藏品编号重复");
        }
        // 转赠的件数
        BigDecimal number = BigDecimal.valueOf(nftNumberStr.size());
        // 校验是否超过单次最低转赠数量
        if (config.getMinTransferNum() != null && nftNumberStr.size() < config.getMinTransferNum()) {
            throw new ServiceException("转赠数量不足");
        }
        // 校验是否超过单次最高转赠数量
        if (config.getMaxTransferNum() != null && nftNumberStr.size() > config.getMaxTransferNum()) {
            throw new ServiceException("转赠数量超过最大限制");
        }
        // 如果配置的热豆大于0，校验热豆是否与配置相同
        if (config.getHotBeans() != null && config.getHotBeans().compareTo(BigDecimal.ZERO) > 0) {
            if (params.getHotBeans().compareTo(config.getHotBeans().multiply(number)) != 0) {
                throw new ServiceException("提交失败，请重新确认转赠~");
            }
        }
        // 如果配置的扳手大于0，校验扳手是否与配置相同
        if (config.getWrench() != null && config.getWrench().compareTo(BigDecimal.ZERO) > 0) {
            if (params.getWrench().compareTo(config.getWrench().multiply(number)) != 0) {
                throw new ServiceException("提交失败，请重新确认转赠~");
            }
        }
        // 如果配置的力豆大于0，校验力豆是否与配置相同
        if (config.getLidou() != null && config.getLidou().compareTo(BigDecimal.ZERO) > 0) {
            if (params.getLidou().compareTo(config.getLidou().multiply(number)) != 0) {
                throw new ServiceException("提交失败，请重新确认转赠~");
            }
        }
        // 如果配置的是白名单才能转赠，检测当前用户是否在白名单
        if (config.getTransferScope() == 1) {
            if (!config.getWhiteUser().contains(currUserId)) {
                throw new ServiceException("当前用户不在白名单");
            }
        }
        // 校验用户累计转赠上限
        if (config.getTransferUpper() != null && config.getTransferUpper() > 0) {
            // 获取用户转赠藏品的总数量
            int count = consumerTeamGiveRecordService.countByTransfer(userId, params.getNftId(), params.getType().getValue()) + params.getNftNumberStr().size();
            if (count > config.getTransferUpper()) {
                throw new ServiceException("用户累计转赠达到上限");
            }
        }
        // 校验钱包密码
        userService.checkPayPwd(userId, params.getPayPwd());
        params.setNftId(config.getSourceId());
        params.setHotBeans(config.getHotBeans().multiply(number));
        params.setWrench(config.getWrench().multiply(number));
        params.setLidou(config.getLidou().multiply(number));
        return params;
    }
}